package jianzhi

import "sort"

func kthLargest(root *TreeNode, k int) int {

	if root == nil {
		return -1
	}

	nums := []int{}

	var dfs func(node *TreeNode)

	dfs = func(node *TreeNode) {
		if node == nil {
			return
		}

		dfs(node.Left)
		nums = append(nums, node.Val)
		dfs(node.Right)

	}

	dfs(root)

	sort.Ints(nums)

	return nums[len(nums)-k]

}
